package so.sao.code.service.web.api;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import so.sao.code.dto.response.TagBatchPrintFileDto;
import so.sao.code.service.infrastructure.enums.PrintFileDownloadMethod;
import so.sao.code.service.service.RangeActivationService;
import so.sao.code.service.service.TagBatchService;
import so.sao.code.service.service.TerminalDownloadResultService;

@Api(tags = "tagbatch", value = "微服务内部标签批次相关接口")
@RestController
@RequestMapping("api/tagbatch")
public class TagBatchFeignController {

	@Autowired
	public TagBatchService tagBatchService;
	@Autowired
	RangeActivationService rangeActivationService;
	@Autowired
	TerminalDownloadResultService terminalDownloadResultService;
	/**
	 * 供应链下载标签数据文件接口
	 *
	 * @param eseId
	 *            企业ID
	 * @param factoryId
	 *            工厂ID
	 * @return TagBatchPrintFileDto 批次ID、批次类型、标签数量、文件url、企业ID、工厂ID
	 */
	@ApiOperation(value = "标签批次下载接口", notes = "供应链下载标签数据文件接口")
	@ApiImplicitParam(name = "eseId", value = "企业ID", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "download/{eseId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResponseEntity<TagBatchPrintFileDto> download(@PathVariable @NotNull Long eseId) {
		TagBatchPrintFileDto result = tagBatchService.downloadTagBatchPrintFileByEseId(eseId,
				PrintFileDownloadMethod.OnLine.getValue());
		if (result == null || StringUtils.isBlank(result.getFileUrl())) {
			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity<TagBatchPrintFileDto>(result, HttpStatus.OK);
	}

	@ApiOperation(value = "获取企业创建码量", notes = "获取企业创建码量")
	@ApiImplicitParam(name = "eseId", value = "企业ID", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "code/{eseId}/count", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResponseEntity<Long> codeCount(@PathVariable @NotNull Long eseId) {
		Long count = tagBatchService.CodeCount(eseId);
		return new ResponseEntity<Long>(count, HttpStatus.OK);
	}

	@ApiOperation(value = "码批次推供应链", notes = "码批次推供应链")
	@ApiImplicitParam(name = "tagBatchId", value = "tagBatchId", required = true, dataType = "Long")
	@PostMapping(value = "pushSupplyChain")
	public ResponseEntity push(@RequestParam("tagbatchId") Long tagbatchId) {
		rangeActivationService.pushSupplyChain(tagbatchId);
		return new ResponseEntity<>(HttpStatus.OK);
	}
}
